package com.ruoyi.productCheck.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import com.ruoyi.product.mapper.ProductMapper;
import com.ruoyi.productCheck.domain.CheckInformation;
import com.ruoyi.productCheck.domain.ProductReport;
import com.ruoyi.productCheck.mapper.CheckInformationMapper;
import com.ruoyi.productCheck.mapper.ProductReportMapper;
import com.ruoyi.productCheck.service.ProductReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class ProductReportServiceImpl implements ProductReportService {
    @Autowired
    ProductReportMapper productReportMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Autowired
    CheckInformationMapper checkInformationMapper;

    @Override
    public int addReport(ProductReport productReport) {
        int num = 0;
        Map<String, Object> resMap = new HashMap<>();
        try {
            num = productReportMapper.addReport(productReport);
            MtAudit mtAudit = new MtAudit();
            mtAudit.setCompanyId(SecurityUtils.getDeptId());
            mtAudit.setOrderId(productReport.getReportId());
            mtAudit.setOrderType("14");
            mtAudit.setOrderNum(productReport.getReportOrderNum());
            mtAudit.setAuditStatus("0");
            mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
            mtAuditMapper.insertMtAudit(mtAudit);
            CheckInformation checkInformation = new CheckInformation();
            checkInformation.setCheckId(productReport.getCheckId());
            checkInformation.setIsCreatReport("1");
            checkInformationMapper.updateInformation(checkInformation);
        } catch (Exception e) {
            resMap.put("orderId", productReport.getReportId());
            resMap.put("orderType", "14");
            mtAuditMapper.deleteMtAuditById(resMap);
            CheckInformation checkInformation = new CheckInformation();
            checkInformation.setCheckId(productReport.getCheckId());
            checkInformation.setIsCreatReport("0");
            checkInformationMapper.updateInformation(checkInformation);
            productReportMapper.removeReport(productReport.getReportId());
        }
        return num;
    }

    @Override
    public void removeReport(Long reportId) {
        productReportMapper.removeReport(reportId);
    }

    @Override
    public List<ProductReport> reportList(ProductReport productReport) {
        return productReportMapper.reportList(productReport);
    }

    @Override
    public ProductReport selectReport(Long reportId) {
        return productReportMapper.selectReport(reportId);
    }

    @Override
    public void remove(Long reportId) {
        productReportMapper.removeReport(reportId);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("orderId", reportId);
        resMap.put("orderType", "14");
        mtAuditMapper.deleteMtAuditById(resMap);
        ProductReport productReport = productReportMapper.selectReport(reportId);
        CheckInformation checkInformation = new CheckInformation();
        checkInformation.setCheckId(productReport.getCheckId());
        checkInformation.setIsCreatReport("0");
        checkInformationMapper.updateInformation(checkInformation);
    }

    @Override
    public List<ProductReport> auditReport(Long deptId) {
        return productReportMapper.auditReport(deptId);
    }

    @Override
    public int updateReport(ProductReport productReport) {
        int num = productReportMapper.updateReport(productReport);
        return num;
    }



}
